package com.itheima.reggie.controller;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/setmeal")
@RestController
public class SetmealController {
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    SetmealService setmealService;

    //新增套餐接口
    @PostMapping
    @Transactional
    public R addSetmeal(@RequestBody SetmealDto setmealDto) {
        //添加套餐
        setmealService.save(setmealDto);
        //关联菜品
        //取出套餐id
        Long id = setmealDto.getId();
        //取出菜品id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            Long dishId = setmealDish.getDishId();
            setmealDish.setSetmealId(id);
            setmealDishService.save(setmealDish);
        }

        return R.success("新增成功");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        //分页查询
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        //条件查询
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        //lqw.like(name != null,Setmeal::getName,name);
        lqw.orderByAsc(Setmeal::getUpdateTime);
        //List<Setmeal> list = setmealService.list(lqw);
        setmealService.page(page1, lqw);
        return R.success(page1);
    }

    //    删除套餐
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        setmealService.delete1(ids);
        return R.success("删除成功");
    }

    //    起售/停售套餐
    @PostMapping("/status/{status}")
    public R status(@PathVariable int status, @RequestParam List<Long> ids) {
        //条件设置
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getStatus, status);
        lqw.in(Setmeal::getId, ids);
        //更新套餐
        boolean update = setmealService.update(lqw);
        if (!update) {
            throw new CustomException("修改失败");
        }
        return R.success("修改成功");
    }


//修改套餐
    @PutMapping ()
    public R update(@RequestBody SetmealDto setmealDto){
       setmealService.updateSetema(setmealDto);
        return  R.success("修改成功");
    }

    //根据套餐id查询套餐数据

    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        //获取套餐
        Setmeal setmealById = setmealService.getById(id);
    //    查询菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(lqw);
    //    设置setdishdish
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmealById,setmealDto);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

//根据套餐分类id查询该套餐分类下的套餐
    @GetMapping("/list")
    public  R list(long categoryId,int status){
        //添加查找条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId);
        lqw.eq(Setmeal::getStatus,status);
        lqw.orderByAsc(Setmeal::getUpdateTime);
        //查寻套餐分类下的套餐集合
        List<Setmeal> setmealList = setmealService.list(lqw);
        return R.success(setmealList);
    }





}
